package com.yang.IO;

import org.junit.Test;

import java.io.*;

/**
 * @Description: 比较BufferStream方式与FileStream方式效率
 * @Author: YangTing
 * @Date: 2024-05-06 21:47
 */

public class CopyFileTest {

    /**
     * 对比二者拷贝时间
     */
    @Test
    public void compareTime(){
        long start = System.currentTimeMillis();
        String src = "D:\\ediary笔记备份_JavaSE\\new\\最新版笔记.2024-04-01-193813.edf";
        String desc = "D:\\ediary笔记备份_JavaSE\\new\\最新版笔记3.2024-04-01-193813.edf";
//        this.copyWithFileStream(src,desc);//70162
        this.copyWithBufferStream(src,desc);//9931
        long end = System.currentTimeMillis();
        System.out.println("花费的时间为 = " + (end - start));
    }

    /**
     * 拷贝文件通过BufferStream方式
     * @param src
     * @param desc
     */
    public void copyWithBufferStream(String src, String desc){
        File srcFile = new File(src);
        File descFile = new File(desc);
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(descFile);
            bis = new BufferedInputStream(fileInputStream);
            bos = new BufferedOutputStream(fileOutputStream);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = bis.read(buffer)) != -1){
                bos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fileInputStream != null;
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                assert fileOutputStream != null;
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 拷贝文件通过FileStream方式
     * @param src
     * @param desc
     */
    public void copyWithFileStream(String src, String desc){
        File srcFile = new File(src);
        File descFile = new File(desc);
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(descFile);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1){
                fileOutputStream.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fileInputStream != null;
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                assert fileOutputStream != null;
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
